//
//  AddWordView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/26.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit
import SnapKit
class AddWordView: UITextView
{

    //在这之上添加placeholderLabel作为站位文字使用
    private lazy var placeholderLabel:UILabel = {
       
        let lab = UILabel()
        
        lab.numberOfLines = 0
        
        lab.textColor = UIColor.grayColor()
        
        
        return lab
    }()
    
    
    //定义一个属性，从外界来设置站位文字
    var placeHolderText:String?{
        
        didSet{
            self.placeholderLabel.text = placeHolderText
        }
    }
    
    //重写父类已有的属性，监听改变
   override var font: UIFont?{
        didSet{
            
            //设置站位文字的字体大小与textView的站位文字大小相等
            self.placeholderLabel.font = font
            
        }
    }
    
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI()
    {
        self.backgroundColor = UIColor.whiteColor()
        
        //添加站位文字
        addSubview(self.placeholderLabel)
        
        //设置站位文字的约束
        placeholderLabel.snp_makeConstraints { (make) in
            //leading:5
            make.leading.equalTo(self).offset(5)
            //top:8
            make.top.equalTo(self).offset(8)
            //trailing:-10
            make.width.equalTo(SCREENW-10)
        }
        
        
        //通过监听系统发出的有关textView文字改变的通知来设置展位文字的隐藏和显示
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AddWordView.valueChanged), name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    //监听文字的改变来设置站位文字的隐藏和显示
    func valueChanged()
    {
        placeholderLabel.hidden = self.hasText()
    }
    
    //移除监听对象
    deinit
    {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}
